package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.IntegralRegisterBo;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn000101Bo;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.IntegralRegister;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

public class IntegralManagementInputResultController extends TmsController {
	private Logger logger = Logger
	.getLogger(IntegralManagementInputResultController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			TransferRgInputViewHelper view = (TransferRgInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
			Member member = new Member();
			Attn attn = new Attn();
			TmsUserProfile profile = uc.getUserProfile();
			IntegralRegister register = new IntegralRegister();
			register.setMemberId(view.getMemberId());
			register.setMemberType(view.getMemberType());
			register.setSerial(view.getSerial());
			register.setLicensePlate(view.getLicensePlate());
			register.setChargeType(2);
			register.setTransferPrice(Integer.parseInt(StringUtil.removeDH(view.getTransferPrice())));
			register.setPlusMinus(1);
			//register.setRemark("积分补加 "+view.getRemark());//20160313
			register.setRemark("过户缴费人工登记积分 ");
			register.setInsertUserId(profile.getUniqueId());
			
//			if (view.getMemberType() == 1) {
//				member.setMemId(Integer.parseInt(view.getMemberId()));
//				String wp = String.valueOf(Integer.parseInt(StringUtil.removeDH(view.getTransferPrice())) * Double.parseDouble(Registry.getConfig(TmsConstants.WWW_JIFEN_MEMBER_KEY)));
//				if(wp.indexOf(".")!=-1)
//				{
//					wp = wp.substring(0, wp.indexOf("."));
//				}
//				register.setIntegral(Integer.parseInt(wp));
//			}
//			if (view.getMemberType() == 2) {
//				attn.setAttnId(view.getMemberId());
//				String wp = String.valueOf(Integer.parseInt(StringUtil.removeDH(view.getTransferPrice())) * Double.parseDouble(Registry.getConfig(TmsConstants.WWW_JIFEN_ATTN_KEY)));
//				if(wp.indexOf(".")!=-1)
//				{
//					wp = wp.substring(0, wp.indexOf("."));
//				}
//				register.setIntegral(Integer.parseInt(wp));
//			}
//			register.insertRegisterInfo(register);
			
			int pp = Integer.parseInt(StringUtil.removeDH(view.getTransferPrice()));
			if (view.getMemberType() == 1) {
				// 会员
				member = member.findCarScoreByMemberId(view.getMemberId());
				member.setMemId(Integer.parseInt(view.getMemberId()));
				//modify by huangheli at 20151012 start
				//String wp = String.valueOf(pp * Double.parseDouble(Registry.getConfig(TmsConstants.WWW_JIFEN_MEMBER_KEY)));
				String wp = String.valueOf(pp * member.getIntegralRatio());
				//modify by huangheli at 20151012 end
				if(wp.indexOf(".")!=-1)
				{
					wp = wp.substring(0, wp.indexOf("."));
				}
				member.setCardScore(member.getCardScore() + Integer.parseInt(wp));
				member.setUpdateUserId(profile.getUniqueId());
				member.updateIntegralCardScore(member);
				//add by huangheli  at 20151012 start
				register.setIntegral(Integer.parseInt(wp));
				//add by huangheli  at 20151012 end
			}
			if (view.getMemberType() == 2) {
				// 经办人
				attn = attn.findAttnByAttnId(view.getMemberId());
				attn.setAttnId(view.getMemberId());
				//modify by huangheli at 20151012 start
				//String wp = String.valueOf(pp * Double.parseDouble(Registry.getConfig(TmsConstants.WWW_JIFEN_ATTN_KEY)));
				String wp = String.valueOf(pp * attn.getIntegralRatio());
				//modify by huangheli at 20151012 end
				if(wp.indexOf(".")!=-1)
				{
					wp = wp.substring(0, wp.indexOf("."));
				}
				attn.setCardScore(attn.getCardScore() + Integer.parseInt(wp));
				attn.setUpdateUserId(profile.getUniqueId());
				attn.updateIntegralCardScore(attn);
				//add by huangheli  at 20151012 start
				register.setIntegral(Integer.parseInt(wp));
				//add by huangheli  at 20151012 end
			}
			register.insertRegisterInfo(register);
			//add by huangheli at 20151019 start
			//通过API接口将录入信息传送给车代办平台
			/*MobileMessage message = new MobileMessage();
			message.putValue("LicensePlate", register.getLicensePlate());
			message.putValue("MemberId", register.getMemberId());
			message.setMessage("无法同步[积分登记]接口,商户ID为#MemberId#,车牌号为#LicensePlate#");*/
			TransferDataSyn000101Bo m = new TransferDataSyn000101Bo();
			/*m.setMobileMessage(message);
			m.setChargeType(2);*/
			m.setMemberId(register.getMemberId());
			m.setMemberType(register.getMemberType());
			m.setSerial(register.getSerial());
			m.setLicensePlate(register.getLicensePlate());
			m.setIntegral(register.getIntegral());
			m.setTransferPrice(register.getTransferPrice());
			PullingExecutor.getInstance().submit(m);
			//add by huangheli at 20151019 end
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}

}
